<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Custom Control
require_once('../shared/balloon.php');
require_once('../../admin/controls/QEditableLabel.class.php');

class Index extends Layout {

    protected $objEditableArray;
    protected $objContact;
    protected $objPhoneArray;
    protected $objEmailArray;
    protected $objBalloon;
    protected $txtName;
    protected $txtEmail;
    protected $txtPhone;
    protected $txtMessage;
    protected $btnSend;

    protected function Form_Create() {
        parent::Form_Create();

        $this->strTitle = 'Contact';

        $this->CssClass = 'contact-form';
        
        $this->objDefaultWaitIcon->AddCssFile('../../../content/css/contact/index.css');

        $objContentKeyArray = 
                ContentKey::LoadNameValueArrayByContentPageTypeId(ContentPageType::Contact);

        foreach($objContentKeyArray as $key => $objContentKey){
            $this->objEditableArray[$key] = new QEditableLabel($this);
            $this->objEditableArray[$key]->Text = $objContentKey->Value;
            $this->objEditableArray[$key]->ActionParameter = $objContentKey->Id;
            $this->objEditableArray[$key]->ReadOnly = !isset($this->objCurrentUser);
            
            switch ($objContentKey->ContentKeyTypeId) {
                case ContentKeyType::SingleText:
                    $this->objEditableArray[$key]->TextMode = QTextMode::SingleLine;
                    break;
                case ContentKeyType::MultiText:
                    $this->objEditableArray[$key]->TextMode = QTextMode::MultiLine;
                    $this->objEditableArray[$key]->AddCssClass('grid_14');
                    $this->objEditableArray[$key]->Height = '150px';
                    break;
                case ContentKeyType::RichText:
                    $this->objEditableArray[$key]->TextMode = QTextMode::MultiLine;
                    $this->objEditableArray[$key]->AddCssClass('grid_14');
                    $this->objEditableArray[$key]->AddCssClass('wysihtml5');
                    $this->objEditableArray[$key]->Width = '100%';
                    $this->objEditableArray[$key]->Height = '150px';
                    break;
            }
        }
        
        $this->objContact = Contact::Load(1);
        $this->objPhoneArray = $this->objContact->GetContactPhoneArray();
        $this->objEmailArray = $this->objContact->GetContactEmailArray();

        $this->objBalloon = new BalloonControl($this);

        $this->txtName = new QTextBox($this);
        $this->txtName->SetCustomAttribute('placeholder', 'Nombre:');

        $this->txtEmail = new QEmailTextBox($this);
        $this->txtEmail->SetCustomAttribute('placeholder', 'Email:');

        $this->txtPhone = new QTextBox($this);
        $this->txtPhone->SetCustomAttribute('placeholder', 'Tel.:');

        $this->txtMessage = new QTextBox($this);
        $this->txtMessage->SetCustomAttribute('placeholder', 'Mensaje:');
        $this->txtMessage->TextMode = QTextMode::MultiLine;

        $this->txtName->Required =
                $this->txtEmail->Required =
                $this->txtPhone->Required =
                $this->txtMessage->Required = !isset($this->objCurrentUser);

        $this->txtName->LabelForRequiredUnnamed =
                $this->txtEmail->LabelForRequiredUnnamed =
                $this->txtPhone->LabelForRequiredUnnamed =
                $this->txtMessage->LabelForRequiredUnnamed = '* El campo es requerido.';

        $this->btnSend = new QButton($this);
        $this->btnSend->Text = 'enviar';
        $this->btnSend->CssClass = 'button';
        $this->btnSend->PrimaryButton = true;
        $this->btnSend->CausesValidation = true;
        $this->btnSend->AddAction(new QClickEvent(), new QAjaxAction('btnSend_Click'));
        $this->btnSend->AddAction(new QClickEvent(), new QTerminateAction());
    }

    protected function Form_Validate() {

        $blnToReturn = parent::Form_Validate();

        $blnFocused = false;
        foreach ($this->GetErrorControls() as $objControl) {

            if (!$blnFocused) {
                $objControl->Focus();
                $blnFocused = true;
            }

            $objControl->Blink();
        }

        if ($blnToReturn) {
            //QApplication::ExecuteJavaScript(sprintf("fcnBtnToggle('%s',true)", $this->btnSend->ControlId));
        }

        return $blnToReturn;
    }

    protected function btnSend_Click($strFormId, $strControlId, $strParameter) {
        try {
            QEmailServer::Send(
                    new QEmailMessage(
                            QEmailServer::$SmtpUsername,
                            __EMAIL_ADDRESS_TO__,
                            'Mensaje generado desde www.felixayala.com.ar',
                            $this->FormatEmailMessage()
                    )
            );

            //QApplication::ExecuteJavaScript(sprintf("fcnBtnToggle('%s',true)", $strControlId));
            $this->objBalloon->Success('Muchas Gracias!', 'Muy pronto nos pondremos en contacto con usted.');

            $this->txtName->Text    =
            $this->txtEmail->Text   =
            $this->txtPhone->Text   =
            $this->txtMessage->Text = null;
            
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            $this->objBalloon->Error('Oops!', $objExc->getMessage());
        }
    }

    private function FormatEmailMessage() {

        $body = __CONTACT_EMAIL_TPL__;
        $body = str_replace('{0}', $this->txtName->Text, $body);
        $body = str_replace('{1}', $this->txtEmail->Text, $body);
        $body = str_replace('{2}', $this->txtPhone->Text, $body);
        $body = str_replace('{3}', $this->txtMessage->Text, $body);

        return $body;
    }

}

Index::Run('Index', __DOCROOT__ . __SUBDIRECTORY__ . '/view/contact/index.tpl.php');
?>